//
//  SEGExcellentStaffViewModel.m
//  SEGStandard-SEGMessage
//
//  Created by Clavis on 2020/6/2.
//

#import "SEGExcellentStaffViewModel.h"
#import <SEGModel/SEGDigitalManager.h>

@interface SEGExcellentStaffViewModel ()

@property (nonatomic, assign) __block NSInteger pageIndex;
@property (nonatomic, assign) __block NSInteger totalPage;
@property (nonatomic, strong) SEGDigitalManager *digitalManager;
@property (nonatomic, strong) RACSignal *requestListSignal;
@property (nonatomic, strong) RACSignal *requestFavorSignal;

@end

@implementation SEGExcellentStaffViewModel

- (instancetype)init {
    self = [super init];
    if (self) {
        self.totalPage = 1;
    }
    return self;
}


/// 刷新优秀员工列表
/// @param isRefresh 是否是刷新
- (RACSignal *)refreshListRequestSignal:(BOOL)isRefresh {
    if (isRefresh) {
        self.pageIndex = 1;
    }
    return self.requestListSignal;
}

/// 请求优秀员工列表信号量
- (RACSignal *)requestListSignal {
    if (!_requestListSignal) {
        WeakSelf;
        _requestListSignal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            // 判断是否存在下一页
            if (weakSelf.pageIndex != 1 && weakSelf.pageIndex > weakSelf.totalPage) {
                NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey:kNoMoreDataMessage}];
                [subscriber sendError:error];
                [subscriber sendCompleted];
                return nil;
            }
            
            // 请求数据
            [self.digitalManager requestPropertyAppraisalListWithPageIndex:self.pageIndex pageSize:10 finished:^(BOOL isSuccessed, NSString *codeType, NSString *errorMessage) {
                if (isSuccessed) {
                    [subscriber sendNext:weakSelf.digitalManager.propertyAppraisalDatas];
                    weakSelf.totalPage = weakSelf.digitalManager.totalPages;
                    weakSelf.pageIndex ++;
                } else {
                    NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:[codeType integerValue] userInfo:@{NSLocalizedDescriptionKey:errorMessage}];
                    [subscriber sendError:error];
                }
                [subscriber sendCompleted];
            }];
            return nil;
        }];
    }
    return _requestListSignal;
}

/// 请求员工点赞信号量
/// @param emplyeeModel emplyeeModel
- (RACSignal *)requestFavorSignalWithEmplyeeModel:(SEGExcellentStaffModel *)emplyeeModel {
    if (emplyeeModel.likeStatus == 0) {
        return nil;
    }
    
    // 【bugfix 36934】每次信号量需要新创建，不然id带的是上次的id
    WeakSelf;
    self.requestFavorSignal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [weakSelf.digitalManager requestPropertyAppraisalFaverWithEmployeeId:ValueToString(emplyeeModel.excellentEmpeeId) finished:^(BOOL isSuccessed, NSString *codeType, NSString *errorMessage) {
            if (isSuccessed) {
                emplyeeModel.likeStatus = 0;
                emplyeeModel.likeNum += 1;
                [subscriber sendNext:nil];
            } else {
                NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:[codeType integerValue] userInfo:@{NSLocalizedDescriptionKey:errorMessage}];
                [subscriber sendError:error];
            }
            [subscriber sendCompleted];
        }];
        return nil;
    }];
    return self.requestFavorSignal;
}

- (SEGDigitalManager *)digitalManager {
    if (!_digitalManager) {
        _digitalManager = [[SEGDigitalManager alloc] init];
    }
    return _digitalManager;
}

@end
